using System;
using System.Collections;
using System.Text;
using System.Windows.Forms;

namespace ctsu.Admin.Forms.CoreLab
{
	public class EditBatchDlg : System.Windows.Forms.Form
	{
		public delegate void ButtonClickEventHandler(object sender, EditBatchEventArgs e);
		public event ButtonClickEventHandler ButtonClick;


		#region controls
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.CheckBox chk2;
		private System.Windows.Forms.CheckBox chk3;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox Protocol;
		private System.Windows.Forms.TextBox Patient;
		private Syncfusion.Windows.Forms.Tools.DateTimePickerAdv VisitDate;
		private System.Windows.Forms.TextBox Visit;
		private System.Windows.Forms.TextBox VisitTime;
		private System.Windows.Forms.ComboBox SampleType;
		private System.Windows.Forms.Button btnFindNext;
		private System.Windows.Forms.Button btnReplaceAllMatch;
		private System.Windows.Forms.Button btnReplaceAll;
		private System.Windows.Forms.CheckBox chk4;
		private System.Windows.Forms.CheckBox chk5;
		private System.Windows.Forms.CheckBox chk6;
		private System.Windows.Forms.CheckBox chk7;
		private System.Windows.Forms.Button btnReplaceSelected;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnClear;
		private System.Windows.Forms.CheckBox chk8;
		private System.Windows.Forms.TextBox SmallNote;
		private System.ComponentModel.Container components = null;
		#endregion controls

		public EditBatchDlg()
		{
			InitializeComponent();
		}

		//		public bool Initialize()
		//		{
		//			StringBuilder sb = new StringBuilder();
		//			try
		//			{
		//				Forms.Utils.LoadCbo(SampleType,"SampleType_L",true);
		//			}
		//			catch(Exception ex)
		//			{
		//				sb.Append("Could not load sample type list for Edit Batch Dialog");
		//				sb.Append(Environment.NewLine);
		//				sb.Append("Error: " + ex.Message);
		//				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
		//				MessageBox.Show(this, sb.ToString(),"Edit Batch Dialog Error",MessageBoxButtons.OK, MessageBoxIcon.Stop);
		//				return false;
		//			}
		//
		//			this.Protocol.KeyPress +=new KeyPressEventHandler(Forms.Utils.txtNumWholePos_KeyPress);
		//			this.Patient.KeyPress +=new KeyPressEventHandler(Forms.Utils.txtNumWholePos_KeyPress);
		//			return true;
		//		}

		public void ClearControls()
		{
			this.Patient.Text = "";
			this.Protocol.Text = "";
			this.VisitDate.Value = DateTime.Today;
			this.VisitDate.IsNullDate = true;
			this.Visit.Text = "";
			this.VisitTime.Text = "";
			this.SampleType.SelectedIndex = -1;
			this.SampleType.SelectedIndex = -1;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.chkAll = new System.Windows.Forms.CheckBox();
			this.chk2 = new System.Windows.Forms.CheckBox();
			this.chk3 = new System.Windows.Forms.CheckBox();
			this.chk4 = new System.Windows.Forms.CheckBox();
			this.chk5 = new System.Windows.Forms.CheckBox();
			this.chk6 = new System.Windows.Forms.CheckBox();
			this.chk7 = new System.Windows.Forms.CheckBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.chk8 = new System.Windows.Forms.CheckBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.SmallNote = new System.Windows.Forms.TextBox();
			this.btnClear = new System.Windows.Forms.Button();
			this.SampleType = new System.Windows.Forms.ComboBox();
			this.VisitTime = new System.Windows.Forms.TextBox();
			this.Visit = new System.Windows.Forms.TextBox();
			this.VisitDate = new Syncfusion.Windows.Forms.Tools.DateTimePickerAdv();
			this.Patient = new System.Windows.Forms.TextBox();
			this.Protocol = new System.Windows.Forms.TextBox();
			this.btnReplaceSelected = new System.Windows.Forms.Button();
			this.btnFindNext = new System.Windows.Forms.Button();
			this.btnReplaceAllMatch = new System.Windows.Forms.Button();
			this.btnReplaceAll = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate.Calendar)).BeginInit();
			this.SuspendLayout();
			// 
			// chkAll
			// 
			this.chkAll.Location = new System.Drawing.Point(8, 16);
			this.chkAll.Name = "chkAll";
			this.chkAll.Size = new System.Drawing.Size(96, 18);
			this.chkAll.TabIndex = 1;
			this.chkAll.Text = "All columns";
			// 
			// chk2
			// 
			this.chk2.Location = new System.Drawing.Point(8, 40);
			this.chk2.Name = "chk2";
			this.chk2.Size = new System.Drawing.Size(88, 18);
			this.chk2.TabIndex = 2;
			this.chk2.Text = "Protocol";
			// 
			// chk3
			// 
			this.chk3.Location = new System.Drawing.Point(8, 64);
			this.chk3.Name = "chk3";
			this.chk3.Size = new System.Drawing.Size(88, 18);
			this.chk3.TabIndex = 3;
			this.chk3.Text = "Patient";
			// 
			// chk4
			// 
			this.chk4.Location = new System.Drawing.Point(8, 88);
			this.chk4.Name = "chk4";
			this.chk4.Size = new System.Drawing.Size(88, 18);
			this.chk4.TabIndex = 4;
			this.chk4.Text = "Visit Date";
			// 
			// chk5
			// 
			this.chk5.Location = new System.Drawing.Point(8, 112);
			this.chk5.Name = "chk5";
			this.chk5.Size = new System.Drawing.Size(88, 18);
			this.chk5.TabIndex = 5;
			this.chk5.Text = "Visit";
			// 
			// chk6
			// 
			this.chk6.Location = new System.Drawing.Point(8, 136);
			this.chk6.Name = "chk6";
			this.chk6.Size = new System.Drawing.Size(88, 18);
			this.chk6.TabIndex = 6;
			this.chk6.Text = "Visit Time";
			// 
			// chk7
			// 
			this.chk7.Location = new System.Drawing.Point(8, 160);
			this.chk7.Name = "chk7";
			this.chk7.Size = new System.Drawing.Size(104, 18);
			this.chk7.TabIndex = 7;
			this.chk7.Text = "Sample Type";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.chk8);
			this.groupBox1.Controls.Add(this.chkAll);
			this.groupBox1.Controls.Add(this.chk3);
			this.groupBox1.Controls.Add(this.chk4);
			this.groupBox1.Controls.Add(this.chk5);
			this.groupBox1.Controls.Add(this.chk7);
			this.groupBox1.Controls.Add(this.chk6);
			this.groupBox1.Controls.Add(this.chk2);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(120, 216);
			this.groupBox1.TabIndex = 8;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Replace Columns";
			// 
			// chk8
			// 
			this.chk8.Location = new System.Drawing.Point(8, 184);
			this.chk8.Name = "chk8";
			this.chk8.Size = new System.Drawing.Size(96, 18);
			this.chk8.TabIndex = 8;
			this.chk8.Text = "Label Note";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.SmallNote);
			this.groupBox2.Controls.Add(this.btnClear);
			this.groupBox2.Controls.Add(this.SampleType);
			this.groupBox2.Controls.Add(this.VisitTime);
			this.groupBox2.Controls.Add(this.Visit);
			this.groupBox2.Controls.Add(this.VisitDate);
			this.groupBox2.Controls.Add(this.Patient);
			this.groupBox2.Controls.Add(this.Protocol);
			this.groupBox2.Enabled = false;
			this.groupBox2.Location = new System.Drawing.Point(128, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(160, 216);
			this.groupBox2.TabIndex = 9;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Replace All Rows Matching";
			// 
			// SmallNote
			// 
			this.SmallNote.Location = new System.Drawing.Point(8, 186);
			this.SmallNote.Name = "SmallNote";
			this.SmallNote.Size = new System.Drawing.Size(144, 22);
			this.SmallNote.TabIndex = 132;
			// 
			// btnClear
			// 
			this.btnClear.Location = new System.Drawing.Point(80, 16);
			this.btnClear.Name = "btnClear";
			this.btnClear.Size = new System.Drawing.Size(75, 24);
			this.btnClear.TabIndex = 131;
			this.btnClear.Text = "Clear";
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
			// 
			// SampleType
			// 
			this.SampleType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.SampleType.Location = new System.Drawing.Point(8, 160);
			this.SampleType.Name = "SampleType";
			this.SampleType.Size = new System.Drawing.Size(144, 24);
			this.SampleType.TabIndex = 130;
			// 
			// VisitTime
			// 
			this.VisitTime.Location = new System.Drawing.Point(8, 136);
			this.VisitTime.Name = "VisitTime";
			this.VisitTime.Size = new System.Drawing.Size(112, 22);
			this.VisitTime.TabIndex = 129;
			// 
			// Visit
			// 
			this.Visit.Location = new System.Drawing.Point(8, 112);
			this.Visit.Name = "Visit";
			this.Visit.Size = new System.Drawing.Size(112, 22);
			this.Visit.TabIndex = 128;
			// 
			// VisitDate
			// 
			this.VisitDate.BackgroundColor = new Syncfusion.Drawing.BrushInfo();
			this.VisitDate.Border3DStyle = System.Windows.Forms.Border3DStyle.Flat;
			this.VisitDate.BorderColor = System.Drawing.SystemColors.ControlDark;
			this.VisitDate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			// 
			// 
			// 
			this.VisitDate.Calendar.AllowMultipleSelection = false;
			this.VisitDate.Calendar.Culture = new System.Globalization.CultureInfo("");
			this.VisitDate.Calendar.DaysFont = new System.Drawing.Font("Verdana", 8F);
			this.VisitDate.Calendar.DaysHeaderInterior = new Syncfusion.Drawing.BrushInfo(System.Drawing.SystemColors.Window);
			this.VisitDate.Calendar.Dock = System.Windows.Forms.DockStyle.Fill;
			this.VisitDate.Calendar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.VisitDate.Calendar.GridLines = Syncfusion.Windows.Forms.Grid.GridBorderStyle.None;
			this.VisitDate.Calendar.HeaderHeight = 20;
			this.VisitDate.Calendar.HeaderStartColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(128)))), ((int)(((byte)(62)))));
			this.VisitDate.Calendar.Location = new System.Drawing.Point(0, 0);
			this.VisitDate.Calendar.MinValue = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
			this.VisitDate.Calendar.Name = "monthCalendar";
			// 
			// 
			// 
			this.VisitDate.Calendar.NoneButton.Location = new System.Drawing.Point(131, 0);
			this.VisitDate.Calendar.ScrollButtonSize = new System.Drawing.Size(24, 24);
			this.VisitDate.Calendar.SelectedDates = new System.DateTime[] {
        new System.DateTime(2007, 4, 4, 13, 47, 11, 860)};
			this.VisitDate.Calendar.Size = new System.Drawing.Size(203, 174);
			this.VisitDate.Calendar.SizeToFit = true;
			this.VisitDate.Calendar.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
			this.VisitDate.Calendar.TabIndex = 0;
			// 
			// 
			// 
			this.VisitDate.Calendar.TodayButton.Location = new System.Drawing.Point(0, 0);
			this.VisitDate.Calendar.TodayButton.Size = new System.Drawing.Size(131, 20);
			this.VisitDate.Calendar.WeekFont = new System.Drawing.Font("Verdana", 8F);
			this.VisitDate.Calendar.WeekInterior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Vertical, System.Drawing.Color.PeachPuff, System.Drawing.Color.AntiqueWhite);
			this.VisitDate.CalendarFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.VisitDate.DropDownImage = null;
			this.VisitDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
			this.VisitDate.Location = new System.Drawing.Point(8, 88);
			this.VisitDate.Name = "VisitDate";
			this.VisitDate.NullString = "";
			this.VisitDate.ShowCheckBox = false;
			this.VisitDate.Size = new System.Drawing.Size(97, 20);
			this.VisitDate.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
			this.VisitDate.TabIndex = 127;
			this.VisitDate.Value = new System.DateTime(2007, 4, 4, 13, 47, 11, 860);
			// 
			// Patient
			// 
			this.Patient.Location = new System.Drawing.Point(8, 64);
			this.Patient.Name = "Patient";
			this.Patient.Size = new System.Drawing.Size(40, 22);
			this.Patient.TabIndex = 1;
			// 
			// Protocol
			// 
			this.Protocol.Location = new System.Drawing.Point(8, 40);
			this.Protocol.Name = "Protocol";
			this.Protocol.Size = new System.Drawing.Size(40, 22);
			this.Protocol.TabIndex = 0;
			// 
			// btnReplaceSelected
			// 
			this.btnReplaceSelected.Location = new System.Drawing.Point(296, 16);
			this.btnReplaceSelected.Name = "btnReplaceSelected";
			this.btnReplaceSelected.Size = new System.Drawing.Size(168, 23);
			this.btnReplaceSelected.TabIndex = 10;
			this.btnReplaceSelected.Text = "Replace Selected Row(s)";
			this.btnReplaceSelected.Click += new System.EventHandler(this.btnReplaceSelected_Click);
			// 
			// btnFindNext
			// 
			this.btnFindNext.Enabled = false;
			this.btnFindNext.Location = new System.Drawing.Point(296, 80);
			this.btnFindNext.Name = "btnFindNext";
			this.btnFindNext.Size = new System.Drawing.Size(144, 23);
			this.btnFindNext.TabIndex = 12;
			this.btnFindNext.Text = "Find next match";
			// 
			// btnReplaceAllMatch
			// 
			this.btnReplaceAllMatch.Enabled = false;
			this.btnReplaceAllMatch.Location = new System.Drawing.Point(296, 112);
			this.btnReplaceAllMatch.Name = "btnReplaceAllMatch";
			this.btnReplaceAllMatch.Size = new System.Drawing.Size(144, 23);
			this.btnReplaceAllMatch.TabIndex = 13;
			this.btnReplaceAllMatch.Text = "Replace all matching ";
			// 
			// btnReplaceAll
			// 
			this.btnReplaceAll.Location = new System.Drawing.Point(296, 48);
			this.btnReplaceAll.Name = "btnReplaceAll";
			this.btnReplaceAll.Size = new System.Drawing.Size(144, 23);
			this.btnReplaceAll.TabIndex = 14;
			this.btnReplaceAll.Text = "Replace all";
			this.btnReplaceAll.Click += new System.EventHandler(this.btnReplaceAll_Click);
			// 
			// btnClose
			// 
			this.btnClose.Location = new System.Drawing.Point(336, 168);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(75, 23);
			this.btnClose.TabIndex = 15;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// EditBatchDlg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(474, 228);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnReplaceAll);
			this.Controls.Add(this.btnReplaceAllMatch);
			this.Controls.Add(this.btnFindNext);
			this.Controls.Add(this.btnReplaceSelected);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "EditBatchDlg";
			this.Text = "Draw Labels - Batch Editor";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox2.PerformLayout();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate.Calendar)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.VisitDate)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void btnReplaceSelected_Click(object sender, System.EventArgs e)
		{
			EditBatchEventArgs eva;
			eva = SetEventArgs();

			if (!VerifyReplaceColumns(eva))
				return;

			eva.ButtonName = "ReplaceSelected";
			if (ButtonClick != null)
				ButtonClick(this, eva);
		}

		private void btnReplaceAll_Click(object sender, System.EventArgs e)
		{
			EditBatchEventArgs eva;
			eva = SetEventArgs();

			if (!VerifyReplaceColumns(eva))
				return;

			StringBuilder sb = new StringBuilder("You are about to replace 'ALL' rows for selected columns with changes.");
			sb.Append(Environment.NewLine);
			sb.Append("Press 'Yes' to continue.");
			if (MessageBox.Show(this, sb.ToString(), "Confirm To Continue", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
				return;

			eva.ButtonName = "ReplaceAll";
			if (ButtonClick != null)
				ButtonClick(this, eva);
		}

		private bool VerifyReplaceColumns(EditBatchEventArgs eva)
		{
			foreach (bool b in eva.ReplaceColumns)
			{
				if (b)
				{
					return true;
				}
			}
			MessageBox.Show(this, "Select a replacement column", "Select Replacement Column", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return false;
		}

		private EditBatchEventArgs SetEventArgs()
		{
			EditBatchEventArgs e = new EditBatchEventArgs();

			e.ReplaceColumns[0] = chkAll.Checked;
			e.ReplaceColumns[1] = chk2.Checked;
			e.ReplaceColumns[2] = chk3.Checked;
			e.ReplaceColumns[3] = chk4.Checked;
			e.ReplaceColumns[4] = chk5.Checked;
			e.ReplaceColumns[5] = chk6.Checked;
			e.ReplaceColumns[6] = chk7.Checked;
			e.ReplaceColumns[7] = chk8.Checked;

			if (Protocol.Text.Length > 0)
				e.ColumnMatch.Add("Protocol", Convert.ToInt32(Protocol.Text));
			if (Patient.Text.Length > 0)
				e.ColumnMatch.Add("Protocol", Convert.ToInt32(Patient.Text));
			if (!VisitDate.IsNullDate)
				e.ColumnMatch.Add("VisitDate", VisitDate.Value);
			if (Visit.Text.Length > 0)
				e.ColumnMatch.Add("Visit", Visit.Text);
			if (VisitTime.Text.Length > 0)
				e.ColumnMatch.Add("VisitTime", VisitTime.Text);
			if (SampleType.SelectedIndex > -1)
				e.ColumnMatch.Add("SampleType", SampleType.SelectedValue);

			return e;
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			ClearControls();
		}
	}

	public class EditBatchEventArgs
	{
		public string ButtonName = "";
		public bool[] ReplaceColumns = new bool[15];
		public Hashtable ColumnMatch = new Hashtable();

		public EditBatchEventArgs()
		{

		}
	}
}
